package com.csqf.booksys.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebFilter("/*")
public class AuthorityFilter2 implements Filter {

    public AuthorityFilter2() {
        
    }

	
	public void destroy() {
		
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)resp;
		//1、获取所有的URL
		String uri = request.getRequestURI();
		System.out.println(uri);
		if(uri.contains("book") || uri.contains("pages")){
			//获取session
			HttpSession session = request.getSession(false);
			if(session != null){
				Object obj = session.getAttribute("loginedInfo");
				if(obj != null){
					chain.doFilter(request, response);
				}else{
					uri = "/login.jsp";
					response.sendRedirect(request.getContextPath()+uri);
				}
			}else{
				uri = "/login.jsp";
				response.sendRedirect(request.getContextPath()+uri);
			}
		}else{
			chain.doFilter(request, response);
		}
	}

	
	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
